﻿namespace ColumnOrderingSetup.OrderingSetup
{
    using System;
    using System.Text;
    using System.Web.UI;

    using Microsoft.SharePoint;

    public partial class OrderingSetupUserControl : UserControl
    {
        #region Methods

        protected void Page_Load( object sender, EventArgs e )
        {
            loadMessages( );
        }

        protected void runOrdering_Click( object sender, EventArgs e )
        {
            const string COBACKGROUNDTASK = "cobackgroundtask";
            const string TITLE = "Title";

            using ( SPSite spSite = new SPSite( SPContext.Current.Web.Url ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPList spList = spWeb.Lists[ COBACKGROUNDTASK ];
                    if ( spList.Items.Count > 0 )
                    {
                        status.Text = "Ongoing request found. This request was not submitted.";
                    }
                    else
                    {
                        SPListItem item = spList.Items.Add( );
                        item[ TITLE ] = "Department Sync and Reordering";
                        item.Update( );
                        spList.Update( );
                        status.Text = "Request submitted. This may take a few minutes. In a few minutes please refresh the browser for the completion status.";
                    }
                }
            }
        }

        void loadMessages( )
        {
            const string COBACKGROUNDSTATUS = "cobackgroundstatus";

            using ( SPSite spSite = new SPSite( SPContext.Current.Web.Url ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    SPList spList = spWeb.Lists[ COBACKGROUNDSTATUS ];
                    SPQuery spQuery = new SPQuery
                    {
                        Query = Query.Get( Query.All )
                    };
                    SPListItemCollection items = spList.GetItems( spQuery );
                    if ( items.Count > 0 )
                    {
                        StringBuilder sb = new StringBuilder( );
                        foreach ( SPListItem item in items )
                            sb.AppendLine( item.Title );
                        string s = sb.ToString( ).Trim( );
                        sb = null;
                        uiMessage.Text = s;
                    }
                }
            }
        }

        #endregion Methods

        #region Nested Types

        class Query
        {
            #region Fields

            public const string All = "<OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy>";

            #endregion Fields

            #region Methods

            public static string Get( string query, params object[ ] args )
            {
                return string.Format( query, args );
            }

            #endregion Methods
        }

        #endregion Nested Types
    }
}